home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / GRAPHICS / TS32 / COLORPAN.PAS < prev    next >
Pascal/Delphi Source File  |  1996-03-16  |  13KB  |  413 lines

  1. (***************************************************
  2. TColorPanel->TCustomPanel
  3.  
  4. A panel that uses a palette handle provided by a
  5. TColorPalette to render itself in 256 colors.
  6.  
  7. Properties
  8.  
  9. BlackOutline
  10.   Determines whether a single pixel, black outline will surround the control.
  11. ColorIndex
  12.   The palette index of the panel's color.
  13. ColorIndexFont
  14.   The palette index of the panel's font color.
  15. ColorIndexFontDisabled
  16.   The palette index of the panel's font color when disabled.
  17. ColorIndexFontRollover
  18.   The palette index of the panel's font color when the mouse is moved over
  19.   the control (if Rollover is TRUE).
  20. ColorIndexHilight
  21.   The palette index of the panel's highlight border color.
  22. ColorIndexShadow
  23.   The palette index of the panel's shadow border color.
  24. ColorPalette
  25.   The TColorPalette component that this panel will use.
  26. Rollover
  27.   Whether or not the panel supports a "rollover" effect when the mouse is
  28.   moved over it.
  29.  
  30. Procedures
  31.  
  32. ColorPanelActivate
  33.   The form should call this procedure in its OnActivate handler.  It makes
  34.   sure that all TColorPanels are properly rendered.
  35.  
  36. ColorPanelMouseMove
  37.   Controls should call this procedure in their OnMouseMove handler.  It handles
  38.   the rollover effect when moving off of TColorPanels.
  39. ***************************************************)
  40.  
  41. unit ColorPanel;
  42.  
  43. interface
  44.  
  45. uses
  46.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  47.   ExtCtrls, ColorPalette, DsgnIntf;
  48.  
  49. type
  50.  
  51.   TColorPanel = class( TCustomPanel )
  52.   private
  53.      FBlackOutline: boolean;
  54.      FColorIndex: byte;
  55.      FColorIndexFont: byte;
  56.      FColorIndexFontDisabled: byte;
  57.      FColorIndexFontRollover: byte;
  58.      FColorIndexHilight: byte;
  59.      FColorIndexShadow: byte;
  60.      FColorPalette: TColorPalette;
  61.      FRollover: boolean;
  62.      FMouseOver: boolean;
  63.   protected
  64.      procedure Notification( AComponent: TComponent; Operation: TOperation ); override;
  65.      procedure MouseMove( Shift: TShiftState; X, Y: Integer ); override;
  66.      procedure Paint; override;
  67.      procedure SetBlackOutline( b: boolean );
  68.      procedure SetColorIndex( n: byte );
  69.      procedure SetColorIndexFont( n: byte );
  70.      procedure SetColorIndexFontDisabled( n: byte );
  71.      procedure SetColorIndexHilight( n: byte );
  72.      procedure SetColorIndexShadow( n: byte );
  73.      procedure SetColorPalette( cp: TColorPalette );
  74.      procedure SetMouseOver( b: boolean );
  75.      procedure CMEnabledChanged( var Message: TMessage ); message CM_ENABLEDCHANGED;
  76.      property MouseOver: boolean read FMouseOver write SetMouseOver;
  77.   public
  78.      constructor Create( AOwner: TComponent ); override;
  79.   published
  80.      property Align;
  81.      property Alignment;
  82.      property BevelOuter;
  83.      property BevelWidth;
  84.      property Caption;
  85.      property DragCursor;
  86.      property DragMode;
  87.      property Enabled;
  88.      property Font;
  89.      property ParentShowHint;
  90.      property PopupMenu;
  91.      property ShowHint;
  92.      property Visible;
  93.      property BlackOutline: boolean read FBlackOutline write SetBlackOutline default TRUE;
  94.      property ColorIndex: byte read FColorIndex write SetColorIndex;
  95.      property ColorIndexFont: byte read FColorIndexFont write SetColorIndexFont;
  96.      property ColorIndexFontDisabled: byte read FColorIndexFontDisabled write SetColorIndexFontDisabled;
  97.      property ColorIndexFontRollover: byte read FColorIndexFontRollover write FColorIndexFontRollover;
  98.      property ColorIndexHilight: byte read FColorIndexHilight write SetColorIndexHilight;
  99.      property ColorIndexShadow: byte read FColorIndexShadow write SetColorIndexShadow;
  100.      property ColorPalette: TColorPalette read FColorPalette write SetColorPalette;
  101.      property Rollover: boolean read FRollover write FRollover default FALSE;
  102.      property OnClick;
  103.      property OnDblClick;
  104.      property OnDragDrop;
  105.      property OnDragOver;
  106.      property OnEndDrag;
  107.      property OnEnter;
  108.      property OnExit;
  109.      property OnMouseDown;
  110.      property OnMouseMove;
  111.      property OnMouseUp;
  112.      property OnResize;
  113.      property OnStartDrag;
  114.   end;
  115.  
  116.   TColorIndexEditor = class( TIntegerProperty )
  117.   private
  118.   protected
  119.   public
  120.      procedure Edit; override;
  121.      function GetAttributes: TPropertyAttributes; override;
  122.   end;
  123.  
  124. procedure ColorPanelMouseMove;
  125. procedure ColorPanelActivate( frm: TForm );
  126. procedure Register;
  127.  
  128. var
  129.   panelLastRolled: TColorPanel;
  130.  
  131. implementation
  132.  
  133. uses
  134.   ColorIndexEditor;
  135.  
  136. (***************************************************
  137. OnActivate of the Form should call this procedure,
  138. which makes sure all color panels are properly rendered.
  139. ***************************************************)
  140. procedure ColorPanelActivate( frm: TForm );
  141. var
  142.   i: integer;
  143. begin
  144.   for i := 0 to frm.ComponentCount - 1 do
  145.      if frm.Components[i] is TColorPanel then
  146.         with frm.Components[i] as TColorPanel do
  147.            begin
  148.               if Visible then
  149.                  Refresh;
  150.            end;
  151. end;
  152.  
  153. (***************************************************
  154. The other controls of the form should call this method
  155. on their MouseMove events to ensure that the rollover
  156. is properly terminated.
  157. **************************************************)
  158. procedure ColorPanelMouseMove;
  159. begin
  160.   if panelLastRolled <> nil then
  161.      panelLastRolled.MouseOver := FALSE;
  162. end;
  163.  
  164.  
  165. (***************************************************
  166. Set some defaults
  167. ***************************************************)
  168. constructor TColorPanel.Create( AOwner: TComponent );
  169. begin
  170.   inherited Create( AOwner );
  171.   FBlackOutline := TRUE;
  172.   FMouseOver := FALSE;
  173.   FRollover := FALSE;
  174. end;
  175.  
  176. (***************************************************
  177. Paint by using the specified color indices
  178. ***************************************************)
  179. procedure TColorPanel.Paint;
  180. var
  181.   nColor1, nColor2: byte;
  182.   nBlackOffset: integer;
  183.   rectDraw: TRect;
  184.   nFlags: integer;
  185. begin
  186.   if Assigned( FColorPalette ) then
  187.      begin
  188.         SelectPalette( Canvas.Handle, FColorPalette.Palette, FALSE );
  189.         RealizePalette( Canvas.Handle );
  190.         Canvas.Brush.Style := bsSolid;
  191.         Canvas.Brush.Color := PaletteIndex( FColorIndex );
  192.         Canvas.FillRect( Canvas.ClipRect );
  193.  
  194. { Determine which colors to use for left/upper and right/lower edges }
  195.         if BevelOuter = bvRaised then
  196.            begin
  197.               nColor1 := FColorIndexHilight;
  198.               nColor2 := FColorIndexShadow;
  199.            end
  200.         else
  201.            begin
  202.               nColor1 := FColorIndexShadow;
  203.               nColor2 := FColorIndexHilight;
  204.            end;
  205.  
  206. { If the black border is drawm, offset all coords by 1 pixel }
  207.         if FBlackOutline then
  208.            nBlackOffset := 1
  209.         else
  210.            nBlackOffset := 0;
  211.  
  212. { Draw the hilight }
  213.         Canvas.Pen.Style := psSolid;
  214.         Canvas.Pen.Width := BevelWidth * 2;
  215.         Canvas.Pen.Color := PaletteIndex( nColor1 );
  216.         Canvas.MoveTo( nBlackOffset, Height - 1 - nBlackOffset );
  217.         Canvas.LineTo( nBlackOffset, nBlackOffset );
  218.         Canvas.LineTo( Width - 1 + 1 - nBlackOffset, nBlackOffset );
  219.  
  220. { Draw the shadow }
  221.         Canvas.Pen.Color := PaletteIndex( nColor2 );
  222.         Canvas.LineTo( Width - 1 + 1 - nBlackOffset, Height - 1 + 1 - nBlackOffset );
  223.         Canvas.LineTo( nBlackOffset, Height - 1 + 1 - nBlackOffset );
  224.  
  225. { Draw the black border }
  226.         if FBlackOutline then
  227.            begin
  228.               Canvas.Brush.Style := bsClear;
  229.               Canvas.Pen.Color := PaletteIndex( 0 );
  230.               Canvas.Pen.Width := 1;
  231.               Canvas.Rectangle( 0, 0, Width - 1, Height  - 1 );
  232.            end;
  233.  
  234. { Draw the caption aligned within the client rectangle }
  235.         if Caption <> '' then
  236.            begin
  237.               case Alignment of
  238.                  taCenter:
  239.                     nFlags := DT_SINGLELINE or DT_VCENTER or DT_CENTER;
  240.                  taLeftJustify:
  241.                     nFlags := DT_SINGLELINE or DT_VCENTER or DT_LEFT;
  242.                  taRightJustify:
  243.                     nFlags := DT_SINGLELINE or DT_VCENTER or DT_RIGHT;
  244.               end;
  245.  
  246.               Canvas.Font.Assign( Font );
  247.  
  248.               if not Enabled then
  249.                  Canvas.Font.Color := PaletteIndex( FColorIndexFontDisabled )
  250.               else if MouseOver and Rollover then
  251.                  Canvas.Font.Color := PaletteIndex( FColorIndexFontRollover )
  252.               else
  253.                  Canvas.Font.Color := PaletteIndex( FColorIndexFont );
  254.  
  255.               rectDraw := ClientRect;
  256.               Canvas.Brush.Style := bsClear;
  257.  
  258.               if BevelOuter = bvLowered then
  259.                  OffsetRect( rectDraw, BevelWidth, BevelWidth );
  260.                  
  261.               DrawText( Canvas.Handle, PChar( Caption ), Length( Caption ), rectDraw, nFlags );
  262.            end;
  263.  
  264.      end
  265.   else
  266.      inherited Paint;
  267. end;
  268.  
  269. (***************************************************
  270. When color properties change, refresh the control.
  271. ***************************************************)
  272. procedure TColorPanel.SetBlackOutline( b: boolean );
  273. begin
  274.   if FBlackOutline <> b then
  275.      begin
  276.         FBlackOutline := b;
  277.         Refresh;
  278.      end;
  279. end;
  280.  
  281. procedure TColorPanel.SetColorIndex( n: byte );
  282. begin
  283.   if FColorIndex <> n then
  284.      begin
  285.         FColorIndex := n;
  286.         Refresh;
  287.      end;
  288. end;
  289.  
  290. procedure TColorPanel.SetColorIndexFont( n: byte );
  291. begin
  292.   if FColorIndexFont <> n then
  293.      begin
  294.         if FColorIndexFontRollover = 0 then
  295.            FColorIndexFontRollover := n;
  296.         FColorIndexFont := n;
  297.         Refresh;
  298.      end;
  299. end;
  300.  
  301. procedure TColorPanel.SetColorIndexFontDisabled( n: byte );
  302. begin
  303.   if FColorIndexFontDisabled <> n then
  304.      begin
  305.         FColorIndexFontDisabled := n;
  306.         Refresh;
  307.      end;
  308. end;
  309.  
  310. procedure TColorPanel.SetColorIndexHilight( n: byte );
  311. begin
  312.   if FColorIndexHilight <> n then
  313.      begin
  314.         FColorIndexHilight := n;
  315.         Refresh;
  316.      end;
  317. end;
  318.  
  319. procedure TColorPanel.SetColorIndexShadow( n: byte );
  320. begin
  321.   if FColorIndexShadow <> n then
  322.      begin
  323.         FColorIndexShadow := n;
  324.         Refresh;
  325.      end;
  326. end;
  327.  
  328. procedure TColorPanel.SetColorPalette( cp: TColorPalette );
  329. begin
  330.   FColorPalette := cp;
  331.   Refresh;
  332. end;
  333.  
  334. procedure TColorPanel.CMEnabledChanged(var Message: TMessage);
  335. begin
  336.   inherited;
  337.   Refresh;
  338. end;
  339.  
  340. (***************************************************
  341. If the color panel is deleted, set property to nil.
  342. ***************************************************)
  343. procedure TColorPanel.Notification( AComponent: TComponent; Operation: TOperation );
  344. begin
  345.   if AComponent = FColorPalette then
  346.      if Operation = opRemove then
  347.         ColorPalette := nil;
  348. end;
  349.  
  350. (***************************************************
  351. Handle Rollover effect on MouseMove
  352. ***************************************************)
  353. procedure TColorPanel.MouseMove( Shift: TShiftState; X, Y: Integer );
  354. begin
  355.   inherited MouseMove( Shift, X, Y );
  356.   if panelLastRolled <> nil then
  357.      if panelLastRolled <> self then
  358.         panelLastRolled.MouseOver := FALSE;
  359.   MouseOver := TRUE;
  360.   panelLastRolled := self;
  361. end;
  362.  
  363. procedure TColorPanel.SetMouseOver( b: boolean );
  364. begin
  365.   if FMouseOver <> b then
  366.      begin
  367.         FMouseOver := b;
  368.         if Rollover then
  369.            Refresh;
  370.      end;
  371. end;
  372.  
  373. (*********************************************
  374. Property editors.
  375. *********************************************)
  376. procedure TColorIndexEditor.Edit;
  377. var
  378.   cpSelf: TColorPalette;
  379. begin
  380.   cpSelf := TColorPalette( TColorPanel( GetComponent( 0 ) ).ColorPalette );
  381.   if cpSelf = nil then
  382.      begin
  383.         ShowMessage( 'First assign a TColorPalette to the ColorPalette property' );
  384.         Exit;
  385.      end;
  386.   with TfrmColorIndexEditor.Create( nil ) do
  387.      begin
  388.         pal := cpSelf;
  389.         nIndex := GetOrdValue;
  390.         if ShowModal = mrOk then
  391.            SetOrdValue( nIndex );
  392.         Release;
  393.      end;
  394. end;
  395.  
  396. function TColorIndexEditor.GetAttributes: TPropertyAttributes;
  397. begin
  398.   Result := [paDialog];
  399. end;
  400.  
  401. procedure Register;
  402. begin
  403.   RegisterPropertyEditor( TypeInfo( byte ), TColorPanel, 'ColorIndex', TColorIndexEditor );
  404.   RegisterPropertyEditor( TypeInfo( byte ), TColorPanel, 'ColorIndexFont', TColorIndexEditor );
  405.   RegisterPropertyEditor( TypeInfo( byte ), TColorPanel, 'ColorIndexFontDisabled', TColorIndexEditor );
  406.   RegisterPropertyEditor( TypeInfo( byte ), TColorPanel, 'ColorIndexFontRollover', TColorIndexEditor );
  407.   RegisterPropertyEditor( TypeInfo( byte ), TColorPanel, 'ColorIndexHilight', TColorIndexEditor );
  408.   RegisterPropertyEditor( TypeInfo( byte ), TColorPanel, 'ColorIndexShadow', TColorIndexEditor );
  409.   RegisterComponents( 'Vis', [TColorPanel] );
  410. end;
  411.  
  412. end.
  413.